package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.TestKeysBean;

/**
 * Created by chenchangxing on 15/12/29.
 */
public class CheckAnswerFragment extends Fragment {
    @Bind(R.id.check_answer_type)
    TextView checkAnswerType;
    @Bind(R.id.check_answer_title)
    TextView checkAnswerTitle;
    @Bind(R.id.check_answer_select_icon_a)
    ImageView checkAnswerSelectIconA;
    @Bind(R.id.check_answer_select_answer_a)
    TextView checkAnswerSelectAnswerA;
    @Bind(R.id.check_answer_select_a)
    LinearLayout checkAnswerSelectA;
    @Bind(R.id.check_answer_select_icon_b)
    ImageView checkAnswerSelectIconB;
    @Bind(R.id.check_answer_select_answer_b)
    TextView checkAnswerSelectAnswerB;
    @Bind(R.id.check_answer_select_b)
    LinearLayout checkAnswerSelectB;
    @Bind(R.id.check_answer_select_icon_c)
    ImageView checkAnswerSelectIconC;
    @Bind(R.id.check_answer_select_answer_c)
    TextView checkAnswerSelectAnswerC;
    @Bind(R.id.check_answer_select_c)
    LinearLayout checkAnswerSelectC;
    @Bind(R.id.check_answer_select_icon_d)
    ImageView checkAnswerSelectIconD;
    @Bind(R.id.check_answer_select_answer_d)
    TextView checkAnswerSelectAnswerD;
    @Bind(R.id.check_answer_select_d)
    LinearLayout checkAnswerSelectD;
    @Bind(R.id.check_answer_select_icon_e)
    ImageView checkAnswerSelectIconE;
    @Bind(R.id.check_answer_select_answer_e)
    TextView checkAnswerSelectAnswerE;
    @Bind(R.id.check_answer_select_e)
    LinearLayout checkAnswerSelectE;
    @Bind(R.id.check_answer_select_icon_f)
    ImageView checkAnswerSelectIconF;
    @Bind(R.id.check_answer_select_answer_f)
    TextView checkAnswerSelectAnswerF;
    @Bind(R.id.check_answer_select_f)
    LinearLayout checkAnswerSelectF;
    @Bind(R.id.check_answer_select)
    LinearLayout checkAnswerSelect;
    @Bind(R.id.check_answer_judge_icon_a)
    ImageView checkAnswerJudgeIconA;
    @Bind(R.id.check_answer_judge_answer_a)
    TextView checkAnswerJudgeAnswerA;
    @Bind(R.id.check_answer_judge_a)
    LinearLayout checkAnswerJudgeA;
    @Bind(R.id.check_answer_judge_icon_b)
    ImageView checkAnswerJudgeIconB;
    @Bind(R.id.check_answer_judge_answer_b)
    TextView checkAnswerJudgeAnswerB;
    @Bind(R.id.check_answer_judge_b)
    LinearLayout checkAnswerJudgeB;
    @Bind(R.id.check_answer_judge)
    LinearLayout checkAnswerJudge;

    List<TestKeysBean.DataEntity> dataEntities;
    int position;
    String tid;

    String ttype, right;
    @Bind(R.id.check_answer_detail)
    TextView checkAnswerDetail;

    public CheckAnswerFragment(List<TestKeysBean.DataEntity> dataEntities, int position) {
        this.dataEntities = dataEntities;
        this.position = position;
        tid = dataEntities.get(position).getId();
        ttype = dataEntities.get(position).getTtype();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_check_answer, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        initInfo();
    }

    private TestKeysBean.DataEntity getItem() {
        return dataEntities.get(position);
    }

    private void initInfo() {
        try {
            if (TextUtils.isEmpty(dataEntities.get(position).getExplain())) {
                checkAnswerDetail.setText("无");
            } else {
                checkAnswerDetail.setText(dataEntities.get(position).getExplain());
            }

            checkAnswerTitle.setText(getItem().getTitle());
            int type = Integer.parseInt(getItem().getTtype());
            StringBuffer str = new StringBuffer();
            for (int i = 0; i < getItem().getRight().size(); i++) {
                str.append(getItem().getRight().get(i));

            }
            right = str.toString();

            int choose = Integer.parseInt(getItem().getChoose());
            switch (type) {

                case 1: {
                    checkAnswerType.setText("选择");
                    checkAnswerSelect.setVisibility(View.VISIBLE);
                    checkAnswerJudge.setVisibility(View.GONE);
                    switch (getItem().getOption().size()) {
                        case 3:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectD.setVisibility(View.GONE);
                            checkAnswerSelectE.setVisibility(View.GONE);
                            checkAnswerSelectF.setVisibility(View.GONE);
                            break;
                        case 4:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectAnswerD.setText(getItem().getOption().get(3));
                            checkAnswerSelectE.setVisibility(View.GONE);
                            checkAnswerSelectF.setVisibility(View.GONE);
                            break;
                        case 5:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectAnswerD.setText(getItem().getOption().get(3));
                            checkAnswerSelectAnswerE.setText(getItem().getOption().get(4));
                            checkAnswerSelectF.setVisibility(View.GONE);
                            break;
                        case 6:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectAnswerD.setText(getItem().getOption().get(3));
                            checkAnswerSelectAnswerE.setText(getItem().getOption().get(4));
                            checkAnswerSelectAnswerF.setText(getItem().getOption().get(5));
                            break;
                    }
                    if (Integer.parseInt(right) == choose) {

                        for (int j = 0; j < right.length(); j++) {
                            if (right.charAt(j) == '1') {
                                checkAnswerSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(j) == '2') {
                                checkAnswerSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(j) == '3') {
                                checkAnswerSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(j) == '4') {
                                checkAnswerSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(j) == '5') {
                                checkAnswerSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(j) == '6') {
                                checkAnswerSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                            }
                        }
                    } else {
                        switch (Integer.parseInt(right)) {
                            case 1: {
                                initAnswer(checkAnswerSelectIconA);
                                break;
                            }
                            case 2: {
                                initAnswer(checkAnswerSelectIconB);
                                break;
                            }
                            case 3: {
                                initAnswer(checkAnswerSelectIconC);
                                break;
                            }
                            case 4: {
                                initAnswer(checkAnswerSelectIconD);
                                break;
                            }
                            case 5: {
                                initAnswer(checkAnswerSelectIconE);
                                break;
                            }
                            case 6: {
                                initAnswer(checkAnswerSelectIconF);
                                break;
                            }
                        }
                    }
                    break;
                }
                case 2: {
                    checkAnswerType.setText("判断");
                    checkAnswerJudge.setVisibility(View.VISIBLE);
                    checkAnswerSelect.setVisibility(View.GONE);
                    if (choose == Integer.parseInt(right)) {
                        switch (Integer.parseInt(right)) {
                            case 1: {
                                checkAnswerJudgeIconA.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                            case 2: {
                                checkAnswerJudgeIconB.setImageResource(R.mipmap.exam_answer_right);
                                break;
                            }
                        }
                    } else {
                        switch (Integer.parseInt(right)) {
                            case 1: {
                                initJudgeAnswer(checkAnswerJudgeIconA);
                                break;
                            }
                            case 2: {
                                initJudgeAnswer(checkAnswerJudgeIconB);
                                break;
                            }
                        }
                    }
                    break;
                }
                case 3: {
                    checkAnswerType.setText("不定项");
                    checkAnswerSelect.setVisibility(View.VISIBLE);
                    checkAnswerJudge.setVisibility(View.GONE);
                    int optionSize = 0;
                    for (int i = 0; i < getItem().getOption().size(); i++) {
                        if (getItem().getOption().get(i).equals("") || getItem().getOption().get(i).isEmpty() || getItem().getOption().get(i) == null) {
                            optionSize++;
                        }
                    }
                    switch (optionSize) {
                        case 2:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectC.setVisibility(View.GONE);
                            checkAnswerSelectD.setVisibility(View.GONE);
                            checkAnswerSelectE.setVisibility(View.GONE);
                            checkAnswerSelectF.setVisibility(View.GONE);
                            break;
                        case 3:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectD.setVisibility(View.GONE);
                            checkAnswerSelectE.setVisibility(View.GONE);
                            checkAnswerSelectF.setVisibility(View.GONE);
                            break;
                        case 4:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectAnswerD.setText(getItem().getOption().get(3));
                            checkAnswerSelectE.setVisibility(View.GONE);
                            checkAnswerSelectF.setVisibility(View.GONE);
                            break;
                        case 5:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectAnswerD.setText(getItem().getOption().get(3));
                            checkAnswerSelectAnswerE.setText(getItem().getOption().get(4));
                            checkAnswerSelectF.setVisibility(View.GONE);
                            break;
                        case 6:
                            checkAnswerSelectAnswerA.setText(getItem().getOption().get(0));
                            checkAnswerSelectAnswerB.setText(getItem().getOption().get(1));
                            checkAnswerSelectAnswerC.setText(getItem().getOption().get(2));
                            checkAnswerSelectAnswerD.setText(getItem().getOption().get(3));
                            checkAnswerSelectAnswerE.setText(getItem().getOption().get(4));
                            checkAnswerSelectAnswerF.setText(getItem().getOption().get(5));
                            break;
                    }

                    if (Integer.parseInt(right) == choose) {

                        for (int i = 0; i < right.length(); i++) {
                            if (right.charAt(i) == '1') {
                                checkAnswerSelectIconA.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(i) == '2') {
                                checkAnswerSelectIconB.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(i) == '3') {
                                checkAnswerSelectIconC.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(i) == '4') {
                                checkAnswerSelectIconD.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(i) == '5') {
                                checkAnswerSelectIconE.setImageResource(R.mipmap.exam_answer_right);
                            } else if (right.charAt(i) == '6') {
                                checkAnswerSelectIconF.setImageResource(R.mipmap.exam_answer_right);
                            }
                        }
                    } else {
                        clearAnswer();
                        for (int i = 0; i < right.length(); i++) {
                            if (right.charAt(i) == '1') {
                                addAnswer(checkAnswerSelectIconA);
                            } else if (right.charAt(i) == '2') {
                                addAnswer(checkAnswerSelectIconB);
                            } else if (right.charAt(i) == '3') {
                                addAnswer(checkAnswerSelectIconC);
                            } else if (right.charAt(i) == '4') {
                                addAnswer(checkAnswerSelectIconD);
                            } else if (right.charAt(i) == '5') {
                                addAnswer(checkAnswerSelectIconE);
                            } else if (right.charAt(i) == '6') {
                                addAnswer(checkAnswerSelectIconF);
                            }
                        }

                    }
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initAnswer(ImageView icon) {
        checkAnswerSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
        icon.setImageResource(R.mipmap.exam_answer_right);
    }

    private void addAnswer(ImageView icon) {
        icon.setImageResource(R.mipmap.exam_answer_right);
    }

    private void clearAnswer() {
        checkAnswerSelectIconA.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconB.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconC.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconD.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconE.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerSelectIconF.setImageResource(R.mipmap.exam_answer_wrong);
    }

    private void initJudgeAnswer(ImageView icon) {
        checkAnswerJudgeIconA.setImageResource(R.mipmap.exam_answer_wrong);
        checkAnswerJudgeIconB.setImageResource(R.mipmap.exam_answer_wrong);

        icon.setImageResource(R.mipmap.exam_answer_right);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public String getTid() {
        return tid;
    }

    public String getTtype() {
        return ttype;
    }
}
